package com.mcc.app.productionMat.controller;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.mcc.app.cache.service.IPlcCacheService;
import com.mcc.common.utils.SecurityUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mcc.common.annotation.Log;
import com.mcc.common.core.controller.BaseController;
import com.mcc.common.core.domain.AjaxResult;
import com.mcc.common.enums.BusinessType;
import com.mcc.app.productionMat.domain.RawMaterialConsumption;
import com.mcc.app.productionMat.service.IRawMaterialConsumptionService;
import com.mcc.common.core.page.TableDataInfo;

/**
 * 原料消耗记录
 *
 * @author gxy
 * @date 2025-03-21
 */
@Api(tags = "原料消耗记录")
@RestController
@RequestMapping("/productionMat/consumption")
public class RawMaterialConsumptionController extends BaseController {

    @Autowired
    private IRawMaterialConsumptionService rawMaterialConsumptionService;



    @ApiOperation("查询原料消耗记录列表")
    @ApiOperationSupport(order = 1)
    @PreAuthorize("@ss.hasPermi('productionMat:consumption:list')")
    @GetMapping("/list")
    public AjaxResult<TableDataInfo<RawMaterialConsumption>> list(RawMaterialConsumption rawMaterialConsumption) {
        List<RawMaterialConsumption> list = rawMaterialConsumptionService.selectRawMaterialConsumptionList(rawMaterialConsumption);
        return getDataTable(list);
    }

    @ApiOperation("查询原料消耗记录列表(分页)")
    @ApiOperationSupport(order = 2)
    @PreAuthorize("@ss.hasPermi('productionMat:consumption:list')")
    @GetMapping("/page")
    public AjaxResult<TableDataInfo<RawMaterialConsumption>> page(RawMaterialConsumption rawMaterialConsumption) {
        startPage();
        List<RawMaterialConsumption> list = rawMaterialConsumptionService.selectRawMaterialConsumptionList(rawMaterialConsumption);
        return getDataTable(list);
    }

    @ApiOperation("获取原料消耗记录详细信息")
    @ApiOperationSupport(order = 3)
    @PreAuthorize("@ss.hasPermi('productionMat:consumption:query')")
    @GetMapping("/get/{id}")
    public AjaxResult<RawMaterialConsumption> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(rawMaterialConsumptionService.selectRawMaterialConsumptionById(id));
    }

    @ApiOperation("新增原料消耗记录")
    @ApiOperationSupport(order = 4)
    @PreAuthorize("@ss.hasPermi('productionMat:consumption:add')")
    @Log(title = "原料消耗记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody RawMaterialConsumption rawMaterialConsumption) {
        return toAjax(rawMaterialConsumptionService.insertRawMaterialConsumption(rawMaterialConsumption));
    }

    @ApiOperation("修改原料消耗记录")
    @ApiOperationSupport(order = 5)
    @PreAuthorize("@ss.hasPermi('productionMat:consumption:edit')")
    @Log(title = "原料消耗记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody RawMaterialConsumption rawMaterialConsumption) {
        return toAjax(rawMaterialConsumptionService.updateRawMaterialConsumption(rawMaterialConsumption));
    }

    @ApiOperation("删除原料消耗记录")
    @ApiOperationSupport(order = 6)
    @PreAuthorize("@ss.hasPermi('productionMat:consumption:remove')")
    @Log(title = "原料消耗记录", businessType = BusinessType.DELETE)
	@PostMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(rawMaterialConsumptionService.deleteRawMaterialConsumptionByIds(ids));
    }

    @ApiOperation("查询各料仓物料名称")
    @GetMapping("/getMatrialName")
    public AjaxResult getMatrialName(String tableTag, Integer timeTag, Date startDt, Date endDt) {
        return AjaxResult.success(rawMaterialConsumptionService.geMatrialName());
    }
    @ApiOperation("查询物料历史数据")
    @GetMapping("/getHisMatrialByDate")
    public AjaxResult getHisMatrialByDate(RawMaterialConsumption rawMaterialConsumption) {
        Date startDate = DateUtil.beginOfDay(rawMaterialConsumption.getRawDate());
        Date endDate = DateUtil.endOfDay(rawMaterialConsumption.getRawDate());
        return AjaxResult.success(rawMaterialConsumptionService.getHisMatrialByDate(rawMaterialConsumption.getTableTag(),rawMaterialConsumption.getTimeTag(), startDate,endDate));
    }

    @ApiOperation("获取当天各个班次物料累计值")
    @ApiOperationSupport(order = 2)
//    @PreAuthorize("@ss.hasPermi('productionMat:consumption:list')")
    @GetMapping("/getCurrMatrial")
    public AjaxResult<TableDataInfo<RawMaterialConsumption>> getCurrentMatrial(RawMaterialConsumption rawMaterialConsumption) {
        List<RawMaterialConsumption> list = rawMaterialConsumptionService.getCurrentMaterialList(rawMaterialConsumption);
        return AjaxResult.success(list);
    }
}
